﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/7/2009
 * Time: 8:58 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Buaya.Model;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of ModuleForm.
	/// </summary>
	public partial class ModuleForm : BaseForm
	{
		public ModuleForm() : this(new Module())
		{
			
		}
		
		public ModuleForm(Module m)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			listView1.Resize += delegate { columnHeader2.Width = listView1.Width - 22 - (columnHeader1.Width); };
			Model = m;
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			Module m = Model as Module;
			textBoxName.Text = m.Name;
			checkBoxInactive.Checked = m.Inactive;
			m.CommandAdded += delegate { RefreshList(); };
		}
		
		void RefreshList()
		{
			listView1.Items.Clear();
			foreach (ModuleCommand c in (Model as Module).Commands) {
				ListViewItem li = listView1.Items.Add(c.Name);
				li.SubItems.Add(c.Command);
			}
		}
		
		void ButtonOkClick(object sender, EventArgs e)
		{
			Module m = Model as Module;
			m.Name = textBoxName.Text;
			m.Order = Util.ConvertUtility.ToInt32(textBoxOrder.Text);
			m.Inactive = checkBoxInactive.Checked;
			SessionFacade.SaveOrUpdateModule(m);
			Close();
		}
		
		void ButtonCancelClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void ButtonNewClick(object sender, EventArgs e)
		{
			using (ModuleCommandForm f = new ModuleCommandForm()) {
				if (MainForm.Instance.AddDialog(f) == DialogResult.OK) {
					(Model as Module).AddCommand(f.Model as ModuleCommand);
				}
			}
		}
		
		void ButtonEditClick(object sender, EventArgs e)
		{
			
		}
		
		void ButtonDeleteClick(object sender, EventArgs e)
		{
			if (listView1.SelectedItems.Count > 0) {
				ModuleCommand c = (Model as Module).Commands[listView1.SelectedItems[0].Index];
				(Model as Module).RemoveCommand(c);
			}
		}
	}
}
